﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using IFRS.Web;
using System.Data;
using IFRS.DataAccess.DAL;
using IFRS.Models;
using IFRS_Global;
using System.Drawing;

public partial class IFRS_WIN17_4A : PageBase
{
    #region "---------- Property -----------"

    private readonly SPR_ABOVEBELOWMKTRATE dal_above = new SPR_ABOVEBELOWMKTRATE();
    private AboveBelowMktRate above = new AboveBelowMktRate();

    #endregion

    #region "---------- Events -----------"

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!(Page.IsPostBack || Page.IsCallback))
        {
            this.ViewMenu();
            BindControlData();
            SetErrorMessage();
            BindData();
        }
        else
            ClientScript.RegisterHiddenField("isPostBack", "1");
    }

    protected void btnSAVE_NEW_Click(object sender, EventArgs e)
    {
        SaveData(true);
    }

    protected void btnSAVE_Click(object sender, EventArgs e)
    {
        SaveData(false);
    }

    protected void btnCLEAR_Click(object sender, EventArgs e)
    {
        Response.Redirect(ResolveUrl(Request.Url.AbsoluteUri));
    }

    protected void btnCANCEL_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/IFRS/WIN17_1S.aspx");
    }

    protected void rbtIS_FIXEDRATE_FIX_CheckedChanged(object sender, EventArgs e)
    {
        OnChange_IsFixedRate();
    }

    protected void rbtIS_FIXEDRATE_REF_CheckedChanged(object sender, EventArgs e)
    {
        OnChange_IsFixedRate();
    }

    #endregion

    #region "---------- Methods -----------"

    private void ViewMenu()
    {
        this.MenuTitleBar = Resources.ifrsResource.Add + Resources.ifrsMenu.WIN17;
    }

    private void SetErrorMessage()
    {
        btnSAVE.ValidationGroup = "Master";
        btnSAVE_NEW.ValidationGroup = "Master";
        reqTRANSACTION_DATE.SetRequiredField(lblTRANSACTION_DATE, txtTRANSACTION_DATE, btnSAVE);
        reqMARKET_CODE.SetRequiredField(lblMARKET_CODE, txtMARKET_CODE, btnSAVE);
        
        var msg = string.Format("{0}{1}{2}", Resources.ifrsField.PLEASE_IDENTIFIEND, lblIS_FIXEDRATE.Text, rbtIS_FIXEDRATE_FIX.Text);
        reqFIXED_RATE.SetRequiredField(lblIS_FIXEDRATE, txtFIXED_RATE, btnSAVE, msg);

        msg = string.Format("{0}{1}{2}", Resources.ifrsField.PLEASE_IDENTIFIEND, lblIS_FIXEDRATE.Text, rbtIS_FIXEDRATE_REF.Text);
        reqREFERENCE_INTEREST_CD.SetRequiredField(lblRATIO_ADJUSTMENT, txtREFERENCE_INTEREST_CD, btnSAVE, msg);

        msg = string.Format("{0}{1}{2}", Resources.ifrsField.PLEASE_IDENTIFIEND, lblIS_FIXEDRATE.Text, lblRATIO_ADJUSTMENT.Text);
        reqRATIO_ADJUSTMENT.SetRequiredField(lblRATIO_ADJUSTMENT, txtRATIO_ADJUSTMENT, btnSAVE, msg);
    }

    private void BindControlData()
    {
        rbtIS_FIXEDRATE_FIX.Checked = true;
        btnMARKET_CODE.Attributes.Add("onClick", "return popWindow('" + ResolveUrl("~/PopupPages/MarketCode.aspx") + "','700','550');");
        btnREFERENCE_INTEREST_CD.Attributes.Add("onClick", "return popWindow('" + ResolveUrl("~/PopupPages/ReferenceInterest.aspx") + "','700','550');");

        reqFIXED_RATE.Enabled = true;
        reqREFERENCE_INTEREST_CD.Enabled = false;
        reqRATIO_ADJUSTMENT.Enabled = false;

        txtMARKET_CODE.AddControl(hidMARKET_CODE.ClientID);
        txtMARKET_CODE.AddControl(txtMARKET_CODE.ClientID);
        txtMARKET_CODE.ControlSearch = txtMARKET_CODE.ClientID;
        txtMARKET_CODE.AddQueryString("bIsActive", "1");
        txtMARKET_CODE.SearchKey = "MarketCode_oLNMKTCODE_oMKT_DESC";

        txtREFERENCE_INTEREST_CD.AddControl(hidREFERENCE_INTEREST_CD.ClientID);
        txtREFERENCE_INTEREST_CD.AddControl(txtREFERENCE_INTEREST_CD.ClientID);
        txtREFERENCE_INTEREST_CD.ControlSearch = txtREFERENCE_INTEREST_CD.ClientID;
        txtREFERENCE_INTEREST_CD.AddQueryString("bIsActive", "1");
        txtREFERENCE_INTEREST_CD.SearchKey = "ReferenceInterest_oReferenceInterestCD_oReferenceInterestNM";
    }

    private void BindData()
    {
        if (Request.QueryString["id"] == null)
            return;

        int ID = Convert.ToInt32(Request.QueryString["id"].ToString());
        DataTable dtFCD = dal_above.SEARCH_BYID(ID);
        List<AboveBelowMktRate> AboveList = new List<AboveBelowMktRate>();
        ConvertHelper.ConvertDataTableToObject(dtFCD, AboveList);
        if (dtFCD.Rows.Count > 0)
        {
            txtTRANSACTION_DATE.Text = DateTime.Now.DateToString();
            txtMARKET_CODE.Text = AboveList[0].MarketCode;
            if ((bool)AboveList[0].IsFixedRate.Value)
            {
                txtFIXED_RATE.Text = AboveList[0].FixedRate.DecimalToString();
                reqFIXED_RATE.Enabled = true;
                reqREFERENCE_INTEREST_CD.Enabled = false;
                reqRATIO_ADJUSTMENT.Enabled = false;
            }
            else
            {
                txtREFERENCE_INTEREST_CD.Text = AboveList[0].ReferenceInterestCD;
                txtRATIO_ADJUSTMENT.Text = AboveList[0].RatioAdjustment.DecimalToString();
                reqFIXED_RATE.Enabled = false;
                reqREFERENCE_INTEREST_CD.Enabled = true;
                reqRATIO_ADJUSTMENT.Enabled = true;
            }
            if ((bool)AboveList[0].IsFixedRate.Value)
            {
                rbtIS_FIXEDRATE_FIX.Checked = true;
                rbtIS_FIXEDRATE_REF.Checked = false;
            }
            else
            {
                rbtIS_FIXEDRATE_FIX.Checked = false;
                rbtIS_FIXEDRATE_REF.Checked = true;
            }
        }
    }

    private void SaveData(bool SaveNew)
    {
        try
        {
            above.TransactionDate = txtTRANSACTION_DATE.Text.StringToDateAllowNull();
            above.MarketCode = txtMARKET_CODE.Text;
            above.IsFixedRate = rbtIS_FIXEDRATE_FIX.Checked ? true : false;
            if (rbtIS_FIXEDRATE_FIX.Checked)
            {
                above.FixedRate = txtFIXED_RATE.Text.AsDecimal();
                above.ReferenceInterestCD = null;
                above.RatioAdjustment = null;
            }
            else
            {
                above.FixedRate = null;
                above.ReferenceInterestCD = txtREFERENCE_INTEREST_CD.Text;
                above.RatioAdjustment = txtRATIO_ADJUSTMENT.Text.AsDecimal();
            }
            above.IsConfirmed = false;
            above.SetStandardField();

            int iID = dal_above.INSERT(above);
            if (iID > 0)
            {
                if (SaveNew)
                    this.JS_Alert(Resources.ifrsResource.SaveData, ResolveUrl(Request.Url.AbsolutePath));
                else
                    this.JS_Alert(Resources.ifrsResource.SaveData, ResolveUrl(string.Format("~/IFRS/WIN17_2E.aspx?&id={0}", iID)));
            }
            else
            {
                if (iID == -1)
                    Utility.AlertMessage(Page, Resources.ifrsResource.Duplicate);
                else
                    Utility.AlertMessage(Page, Resources.ifrsResource.CanNotSaveData);
            }

        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    private void OnChange_IsFixedRate()
    {
        if (rbtIS_FIXEDRATE_FIX.Checked)
        {
            txtREFERENCE_INTEREST_CD.Text = "";
            hidREFERENCE_INTEREST_CD.Value = "";
            txtRATIO_ADJUSTMENT.Text = "";
            reqFIXED_RATE.Enabled = true;
            reqREFERENCE_INTEREST_CD.Enabled = false;
            reqRATIO_ADJUSTMENT.Enabled = false;
        }
        else
        {
            txtFIXED_RATE.Text = "";
            reqFIXED_RATE.Enabled = false;
            reqREFERENCE_INTEREST_CD.Enabled = true;
            reqRATIO_ADJUSTMENT.Enabled = true;
        }
    }

    #endregion

    
}